/**
 * Copyright (c) 2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

var Dialog = {};

Dialog.Base = new Class(
{
	Implements: Options,
	
	options: {
		'className': 'dialog',
		'onDisplay': $empty,
		'onHide':    $empty,
		'onDestroy': $empty,
		'titlebar':  false,
		'title':     ''
	},
	
	initialize: function(options)
	{
		// options
		this.setOptions(options);
		
		// the dialog
		this.dialog  = new Element('div', {
			'class': this.options.className,
			'styles': {
				'display': 'none'
			}
		});
		if (this.options.id) {
			this.dialog.id = this.options.id;
		}
		
		// titlebar and content is wrapped in a container 
		this.wrapper = new Element('div', {'class': 'wrapper'});
		
		// titlebar
		if (this.options.titlebar)
		{
			this.titlebar = new Element('div', {'class': 'titlebar'});
			
			// title
			var text = new Element('span', {'class': 'text'}).set('text', this.options.title);
			this.titlebar.appendChild(text);
			
			// buttons (close)
			var close = new Element('a', {'class': 'close'})
				.addEvent('click', this.destroy.bind(this))
				.set('text', trans.ct('dialog', 'close'));
			var buttons = new Element('span', {'class': 'buttons'});
			buttons.appendChild(close);
			this.titlebar.appendChild(buttons);
			
			this.wrapper.appendChild(this.titlebar);
		}
		
		// content
		this.content = new Element('div', {
			'class': 'content'
		});
		this.wrapper.appendChild(this.content);
		
		this.dialog.appendChild(this.wrapper);
		this._injectDialog();
		this._setPosition();
	},
	
	display: function()
	{
		this.options.onDisplay();
		this._setPosition();
		this.dialog.setStyle('display', 'block');
	},
	
	hide: function()
	{
		this.options.onHide();
		this.dialog.setStyle('display', 'none');
	},
	
	destroy: function()
	{
		this.options.onDestroy();
		this.dialog.destroy();
	},
	
	setContent: function(type, content)
	{
		if (type == 'element')
		{
			this.content.empty();
			this.content.appendChild(content);
		}
		else
		{
			this.content.set(type, content);
		}
	},
	
	_injectDialog: function()
	{
		$(document.body).appendChild(this.dialog);
	},
	
	_setPosition: function(left, top, width, height)
	{
		var styles = {'position': 'absolute'};
		if (left)   styles.left   = left   + 'px';
		if (top)    styles.top    = top    + 'px';
		if (width)  styles.width  = width  + 'px';
		if (height) styles.height = height + 'px';
		this.dialog.setStyles(styles);
	}
});
